package com.zkthink.ceres.common.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * <p>
 *
 * </p>
 *
 * @author vivi.huang
 * @since 2020/5/31
 */
public class DesensitizeUtil {

    public DesensitizeUtil() {
    }

    public static String desensitize(String data) {
        if (StringUtils.isBlank(data)) {
            return data;
        } else if (data.length() <= 2) {
            return onLessThan2(data);
        } else {
            return data.length() < 12 ? onGreaterThan2LessThan12(data) : onGreaterThan12(data);
        }
    }

    private static String onLessThan2(String data) {
        return StringUtils.overlay(data, "*", data.length() - 1, data.length());
    }

    private static String onGreaterThan2LessThan12(String data) {
        double overlay = (double) data.length() / 3.0D;
        int overlayCount = overlay > (double) ((int) overlay) ? (int) overlay + 1 : (int) overlay;
        int start = (int) overlay;
        int end = start + overlayCount;
        return StringUtils.overlay(data, StringUtils.repeat("*", overlayCount), start, end);
    }

    private static String onGreaterThan12(String data) {
        double overlay = (double) data.length() / 2.0D;
        int overlayCount = overlay > (double) ((int) overlay) ? (int) overlay + 1 : (int) overlay;
        int start = (int) (overlay / 2.0D);
        int end = start + overlayCount;
        return StringUtils.overlay(data, StringUtils.repeat("*", overlayCount), start, end);
    }
}